using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace WbsEditor
{
    public partial class WbsPropertiesEditControl : UserControl
    {
        public WbsPropertiesEditControl()
        {
            InitializeComponent();
        }

        private bool _hasTfsOptions;
        private string _ownerListFromProject;
        private WbsTfsClient _tfsClient;
        public WbsTfsClient TfsClient { get { return _tfsClient; } set { _tfsClient = value; } }

        public TextBox Title { get { return _title; } }
        public TextBox TID { get { return _tid; } }
        public ComboBox Owner { get { return _owner; } }
        public TextBox LinkToConcept { get { return _linkToConcept; } }
        public WorkBreakdownStructure.TimeUnit DisplayUnit
        {
            get
            {
                if (_displayUnitCombo.Text.Equals("Hours"))
                {
                    return WorkBreakdownStructure.TimeUnit.Hours;
                }
                return WorkBreakdownStructure.TimeUnit.Days;
            }
            set
            {
                if (WorkBreakdownStructure.TimeUnit.Hours == value)
                {
                    _displayUnitCombo.Text = "Hours";
                }
                else
                {
                    _displayUnitCombo.Text = "Days";
                }
            }
        }
        public WorkBreakdownStructure.TimeUnit PersistUnit
        {
            get
            {
                if (_persistUnitCombo.Text.Equals("Hours"))
                {
                    return WorkBreakdownStructure.TimeUnit.Hours;
                }
                return WorkBreakdownStructure.TimeUnit.Days;
            }
            set
            {
                if (WorkBreakdownStructure.TimeUnit.Hours == value)
                {
                    _persistUnitCombo.Text = "Hours";
                }
                else
                {
                    _persistUnitCombo.Text = "Days";
                }
            }
        }
        public float DaysToHoursFactor
        {
            get
            {
                float factor = 8f;
                if (float.TryParse(_daysToHoursText.Text, out factor))
                {
                    return factor;
                }
                return 8f;
            }
            set
            {
                _daysToHoursText.Text = string.Format("{0}", value);
            }
        }

        public void DisplayControl()
        {
            InitControls();
            Visible = true;
            _title.Focus();
        }

        private void InitControls()
        {
            bool readTfsOptions =
                !_hasTfsOptions &&
                TfsClient.IsConnected &&
                !string.IsNullOrEmpty(TfsClient.TeamProject);
            bool refreshTfsOptions =
                _hasTfsOptions &&
                TfsClient.IsConnected &&
                (_ownerListFromProject != TfsClient.TeamProject);
            if (readTfsOptions || refreshTfsOptions)
            {
                // Read Owner drop down list
                _owner.Items.Clear();
                string[] assignedToNames = TfsClient.GetPossibleAssignedToNames();
                _owner.Items.AddRange(assignedToNames);
                _ownerListFromProject = TfsClient.TeamProject;
                // Remember that we have read from the TFS
                _hasTfsOptions = true;
            }
        }

        public event EventHandler QuitEdit;

        private void Controls_OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter
                || e.KeyCode == Keys.Escape)
            {
                QuitEdit(this, new EventArgs());
                e.Handled = true;
            }
        }
    }
}
